# Copyright 2018- The Pixie Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0

load("//bazel:pl_build_system.bzl", "pl_cc_binary", "pl_cc_bpf_test", "pl_cc_library", "pl_cc_test")

package(default_visibility = ["//src/stirling:__subpackages__"])

pl_cc_library(
    name = "cc_library",
    srcs = glob(
        ["*.cc"],
        exclude = [
            "**/*_test.cc",
            "**/*_benchmark.cc",
        ],
    ),
    hdrs = glob(["*.h"]),
    deps = [
        "//src/common/exec:cc_library",
        "//src/common/grpcutils:cc_library",
        "//src/common/metrics:cc_library",
        "//src/common/system:cc_library",
        "//src/stirling/bpf_tools:cc_library",
        "//src/stirling/core:cc_library",
        "//src/stirling/obj_tools:cc_library",
        "//src/stirling/source_connectors/socket_tracer/bcc_bpf:socket_trace",
        "//src/stirling/source_connectors/socket_tracer/bcc_bpf_intf:cc_library",
        "//src/stirling/source_connectors/socket_tracer/proto:sock_event_pl_cc_proto",
        "//src/stirling/source_connectors/socket_tracer/protocols:cc_library",
        "//src/stirling/utils:cc_library",
    ],
)

pl_cc_test(
    name = "socket_trace_connector_test",
    srcs = ["socket_trace_connector_test.cc"],
    deps = [
        ":cc_library",
        "//src/stirling/source_connectors/socket_tracer/protocols/cql:testing",
        "//src/stirling/source_connectors/socket_tracer/testing:cc_library",
        "//src/stirling/testing:cc_library",
    ],
)

pl_cc_test(
    name = "socket_trace_protocols_test",
    srcs = ["socket_trace_protocols_test.cc"],
    deps = [
        ":cc_library",
        "//src/stirling/source_connectors/socket_tracer/protocols/cql:testing",
        "//src/stirling/source_connectors/socket_tracer/testing:cc_library",
        "//src/stirling/testing:cc_library",
    ],
)

pl_cc_test(
    name = "uprobe_symaddrs_test",
    srcs = ["uprobe_symaddrs_test.cc"],
    data = [
        "//src/stirling/testing/demo_apps/go_grpc_tls_pl/server:golang_1_24_grpc_tls_server_binary",
        "//src/stirling/testing/demo_apps/node:node_debug",
    ],
    deps = [
        ":cc_library",
        "//src/stirling/source_connectors/socket_tracer/testing:cc_library",
        "//src/stirling/utils:cc_library",
    ],
)

pl_cc_test(
    name = "data_stream_test",
    srcs = ["data_stream_test.cc"],
    deps = [
        ":cc_library",
        "//src/stirling/source_connectors/socket_tracer/testing:cc_library",
    ],
)

pl_cc_test(
    name = "conn_tracker_test",
    srcs = ["conn_tracker_test.cc"],
    deps = [
        ":cc_library",
        "//src/stirling/source_connectors/socket_tracer/testing:cc_library",
    ],
)

pl_cc_test(
    name = "conn_trackers_manager_test",
    srcs = ["conn_trackers_manager_test.cc"],
    deps = [
        ":cc_library",
        "//src/stirling/source_connectors/socket_tracer/testing:cc_library",
    ],
)

pl_cc_test(
    name = "conn_tracker_http2_test",
    srcs = ["conn_tracker_http2_test.cc"],
    deps = [
        ":cc_library",
        "//src/stirling/source_connectors/socket_tracer/testing:cc_library",
    ],
)

pl_cc_test(
    name = "conn_stats_test",
    srcs = ["conn_stats_test.cc"],
    deps = [
        ":cc_library",
        "//src/stirling/source_connectors/socket_tracer/testing:cc_library",
    ],
)

pl_cc_test(
    name = "fd_resolver_test",
    srcs = ["fd_resolver_test.cc"],
    deps = [
        ":cc_library",
        "//src/common/system:cc_library",
    ],
)

pl_cc_binary(
    name = "socket_trace_connector_benchmark",
    testonly = 1,
    srcs = ["socket_trace_connector_benchmark.cc"],
    deps = [
        ":cc_library",
        "//src/common/benchmark:cc_library",
        "//src/stirling/source_connectors/socket_tracer/testing:cc_library",
        "//src/stirling/source_connectors/socket_tracer/testing/benchmark_data_gen:cc_library",
        "//src/stirling/testing:cc_library",
    ],
)

###############################################################################
# BPF Tests
###############################################################################

pl_cc_bpf_test(
    name = "socket_trace_bpf_test",
    timeout = "long",
    srcs = ["socket_trace_bpf_test.cc"],
    flaky = True,
    shard_count = 5,
    tags = [
        "cpu:16",
        "requires_bpf",
    ],
    deps = [
        ":cc_library",
        "//src/stirling/source_connectors/socket_tracer/testing:cc_library",
        "//src/stirling/testing:cc_library",
    ],
)

pl_cc_bpf_test(
    name = "conn_stats_bpf_test",
    timeout = "moderate",
    srcs = ["conn_stats_bpf_test.cc"],
    flaky = True,
    shard_count = 2,
    tags = [
        "cpu:16",
        "requires_bpf",
    ],
    deps = [
        ":cc_library",
        "//src/common/exec:cc_library",
        "//src/common/testing/test_utils:cc_library",
        "//src/stirling/source_connectors/socket_tracer/testing:cc_library",
        "//src/stirling/source_connectors/socket_tracer/testing/container_images:curl_container",
        "//src/stirling/source_connectors/socket_tracer/testing/container_images:nginx_openssl_1_1_1_container",
        "//src/stirling/testing:cc_library",
    ],
)

pl_cc_bpf_test(
    name = "http_trace_bpf_test",
    timeout = "moderate",
    srcs = ["http_trace_bpf_test.cc"],
    flaky = True,
    shard_count = 3,
    tags = [
        "cpu:16",
        "no_asan",
        "requires_bpf",
    ],
    deps = [
        ":cc_library",
        "//src/common/exec:cc_library",
        "//src/stirling/source_connectors/socket_tracer/testing:cc_library",
        "//src/stirling/source_connectors/socket_tracer/testing/container_images:curl_container",
        "//src/stirling/testing:cc_library",
        "//src/stirling/testing/demo_apps/go_http:go_http_fixture",
    ],
)

pl_cc_bpf_test(
    name = "mux_trace_bpf_test",
    timeout = "moderate",
    srcs = ["mux_trace_bpf_test.cc"],
    flaky = True,
    tags = [
        "cpu:16",
        "no_asan",
        "requires_bpf",
    ],
    deps = [
        ":cc_library",
        "//src/common/testing/test_utils:cc_library",
        "//src/stirling/source_connectors/socket_tracer/protocols/test_output_generator:cc_library",
        "//src/stirling/source_connectors/socket_tracer/testing:cc_library",
        "//src/stirling/source_connectors/socket_tracer/testing/container_images:thrift_mux_server_container",
        "//src/stirling/testing:cc_library",
    ],
)

pl_cc_bpf_test(
    name = "mysql_trace_bpf_test",
    timeout = "moderate",
    srcs = ["mysql_trace_bpf_test.cc"],
    data = [
        "//src/stirling/source_connectors/socket_tracer/protocols/mysql/testing:expected_outputs",
        "//src/stirling/source_connectors/socket_tracer/protocols/mysql/testing:scripts",
    ],
    flaky = True,
    tags = [
        "cpu:16",
        "no_asan",
        "requires_bpf",
    ],
    deps = [
        ":cc_library",
        "//src/common/testing/test_utils:cc_library",
        "//src/stirling/source_connectors/socket_tracer/protocols/test_output_generator:cc_library",
        "//src/stirling/source_connectors/socket_tracer/testing:cc_library",
        "//src/stirling/source_connectors/socket_tracer/testing/container_images:mysql_container",
        "//src/stirling/source_connectors/socket_tracer/testing/container_images:python_mysql_connector_container",
        "//src/stirling/testing:cc_library",
    ],
)

pl_cc_bpf_test(
    name = "cql_trace_bpf_test",
    timeout = "moderate",
    srcs = [
        "cql_trace_bpf_test.cc",
    ],
    flaky = True,
    tags = [
        "cpu:16",
        "no_asan",
        "requires_bpf",
    ],
    deps = [
        ":cc_library",
        "//src/common/testing/test_utils:cc_library",
        "//src/stirling/source_connectors/socket_tracer/testing:cc_library",
        "//src/stirling/source_connectors/socket_tracer/testing/container_images:cassandra_container",
        "//src/stirling/testing:cc_library",
    ],
)

pl_cc_bpf_test(
    name = "pgsql_trace_bpf_test",
    timeout = "moderate",
    srcs = [
        "pgsql_trace_bpf_test.cc",
    ],
    flaky = True,
    shard_count = 2,
    tags = [
        "cpu:16",
        "no_asan",
        "requires_bpf",
    ],
    deps = [
        ":cc_library",
        "//src/common/testing/test_utils:cc_library",
        "//src/stirling/source_connectors/socket_tracer/testing:cc_library",
        "//src/stirling/source_connectors/socket_tracer/testing/container_images:golang_sqlx_container",
        "//src/stirling/source_connectors/socket_tracer/testing/container_images:postgresql_container",
        "//src/stirling/testing:cc_library",
    ],
)

pl_cc_bpf_test(
    name = "grpc_trace_bpf_test",
    timeout = "moderate",
    srcs = ["grpc_trace_bpf_test.cc"],
    data = [
        "//src/stirling/source_connectors/socket_tracer/protocols/http2/testing/go_grpc_client:golang_1_23_grpc_client",
        "//src/stirling/source_connectors/socket_tracer/protocols/http2/testing/go_grpc_client:golang_1_24_grpc_client",
        "//src/stirling/source_connectors/socket_tracer/protocols/http2/testing/go_grpc_client:golang_boringcrypto_grpc_client",
        "//src/stirling/source_connectors/socket_tracer/protocols/http2/testing/go_grpc_server:golang_1_23_grpc_server_with_certs",
        "//src/stirling/source_connectors/socket_tracer/protocols/http2/testing/go_grpc_server:golang_1_24_grpc_server_with_certs",
        "//src/stirling/source_connectors/socket_tracer/protocols/http2/testing/go_grpc_server:golang_boringcrypto_grpc_server_with_certs",
    ],
    flaky = True,
    shard_count = 3,
    tags = [
        "cpu:16",
        "no_asan",
        "requires_bpf",
    ],
    deps = [
        ":cc_library",
        "//src/common/exec:cc_library",
        "//src/stirling/source_connectors/socket_tracer/protocols/http2/testing:cc_library",
        "//src/stirling/source_connectors/socket_tracer/protocols/http2/testing/proto:greet_pl_cc_proto",
        "//src/stirling/source_connectors/socket_tracer/testing:cc_library",
        "//src/stirling/source_connectors/socket_tracer/testing/container_images:py_grpc_hello_world_container",
        "//src/stirling/testing:cc_library",
    ],
)

pl_cc_bpf_test(
    name = "http2_trace_bpf_test",
    timeout = "moderate",
    srcs = ["http2_trace_bpf_test.cc"],
    flaky = True,
    tags = [
        "cpu:16",
        "no_asan",
        "requires_bpf",
    ],
    deps = [
        ":cc_library",
        "//src/common/exec:cc_library",
        "//src/common/testing/test_utils:cc_library",
        "//src/stirling/source_connectors/socket_tracer/testing:cc_library",
        "//src/stirling/source_connectors/socket_tracer/testing/container_images:go_1_18_grpc_server_container",
        "//src/stirling/source_connectors/socket_tracer/testing/container_images:go_1_19_grpc_server_container",
        "//src/stirling/source_connectors/socket_tracer/testing/container_images:go_1_20_grpc_server_container",
        "//src/stirling/source_connectors/socket_tracer/testing/container_images:go_1_21_grpc_server_container",
        "//src/stirling/source_connectors/socket_tracer/testing/container_images:go_1_22_grpc_server_container",
        "//src/stirling/source_connectors/socket_tracer/testing/container_images:go_1_23_grpc_client_container",
        "//src/stirling/source_connectors/socket_tracer/testing/container_images:go_1_23_grpc_server_container",
        "//src/stirling/source_connectors/socket_tracer/testing/container_images:go_1_24_grpc_client_container",
        "//src/stirling/source_connectors/socket_tracer/testing/container_images:go_1_24_grpc_server_container",
        "//src/stirling/source_connectors/socket_tracer/testing/container_images:go_boringcrypto_grpc_client_container",
        "//src/stirling/source_connectors/socket_tracer/testing/container_images:go_boringcrypto_grpc_server_container",
        "//src/stirling/source_connectors/socket_tracer/testing/container_images:product_catalog_client_container",
        "//src/stirling/source_connectors/socket_tracer/testing/container_images:product_catalog_service_container",
        "//src/stirling/testing:cc_library",
    ],
)

pl_cc_bpf_test(
    name = "dns_trace_bpf_test",
    timeout = "moderate",
    srcs = [
        "dns_trace_bpf_test.cc",
    ],
    flaky = True,
    tags = [
        "cpu:16",
        "no_asan",
        "requires_bpf",
    ],
    deps = [
        ":cc_library",
        "//src/common/testing/test_utils:cc_library",
        "//src/stirling/source_connectors/socket_tracer/testing:cc_library",
        "//src/stirling/source_connectors/socket_tracer/testing/container_images:dns_server_container",
        "//src/stirling/testing:cc_library",
    ],
)

pl_cc_bpf_test(
    name = "redis_trace_bpf_test",
    timeout = "long",
    srcs = ["redis_trace_bpf_test.cc"],
    flaky = True,
    tags = [
        "cpu:16",
        "requires_bpf",
    ],
    deps = [
        ":cc_library",
        "//src/common/exec:cc_library",
        "//src/common/testing/test_utils:cc_library",
        "//src/stirling/source_connectors/socket_tracer/testing:cc_library",
        "//src/stirling/source_connectors/socket_tracer/testing/container_images:redis_client_container",
        "//src/stirling/source_connectors/socket_tracer/testing/container_images:redis_container",
        "//src/stirling/testing:cc_library",
    ],
)

pl_cc_bpf_test(
    name = "nats_trace_bpf_test",
    timeout = "long",
    srcs = ["nats_trace_bpf_test.cc"],
    flaky = True,
    tags = [
        "cpu:16",
        "requires_bpf",
    ],
    deps = [
        ":cc_library",
        "//src/common/testing/test_utils:cc_library",
        "//src/stirling/source_connectors/socket_tracer/testing:cc_library",
        "//src/stirling/source_connectors/socket_tracer/testing/container_images:nats_client_container",
        "//src/stirling/source_connectors/socket_tracer/testing/container_images:nats_server_container",
    ],
)

pl_cc_bpf_test(
    name = "kafka_trace_bpf_test",
    timeout = "moderate",
    srcs = ["kafka_trace_bpf_test.cc"],
    flaky = True,
    tags = [
        "cpu:16",
        "no_qemu",
        "requires_bpf",
    ],
    deps = [
        ":cc_library",
        "//src/common/testing/test_utils:cc_library",
        "//src/stirling/source_connectors/socket_tracer/testing:cc_library",
        "//src/stirling/source_connectors/socket_tracer/testing/container_images:kafka_container",
        "//src/stirling/source_connectors/socket_tracer/testing/container_images:zookeeper_container",
    ],
)

pl_cc_bpf_test(
    name = "openssl_trace_bpf_test",
    timeout = "long",
    srcs = ["openssl_trace_bpf_test.cc"],
    flaky = True,
    shard_count = 2,
    tags = [
        "cpu:16",
        "no_asan",
        "requires_bpf",
    ],
    deps = [
        ":cc_library",
        "//src/common/testing/test_utils:cc_library",
        "//src/stirling/source_connectors/socket_tracer/testing:cc_library",
        "//src/stirling/source_connectors/socket_tracer/testing/container_images:curl_container",
        "//src/stirling/source_connectors/socket_tracer/testing/container_images:nginx_openssl_1_1_0_container",
        "//src/stirling/source_connectors/socket_tracer/testing/container_images:nginx_openssl_1_1_1_container",
        "//src/stirling/source_connectors/socket_tracer/testing/container_images:nginx_openssl_3_0_8_container",
        "//src/stirling/source_connectors/socket_tracer/testing/container_images:node_12_3_1_container",
        "//src/stirling/source_connectors/socket_tracer/testing/container_images:node_14_18_1_alpine_container",
        "//src/stirling/source_connectors/socket_tracer/testing/container_images:node_client_container",
        "//src/stirling/source_connectors/socket_tracer/testing/container_images:python_3_10_container",
        "//src/stirling/source_connectors/socket_tracer/testing/container_images:ruby_container",
        "//src/stirling/testing:cc_library",
    ],
)

pl_cc_bpf_test(
    name = "boringssl_trace_bpf_test",
    timeout = "long",
    srcs = ["boringssl_trace_bpf_test.cc"],
    flaky = True,
    shard_count = 2,
    tags = [
        "cpu:16",
        "no_asan",
        "requires_bpf",
    ],
    deps = [
        ":cc_library",
        "//src/common/testing/test_utils:cc_library",
        "//src/stirling/source_connectors/socket_tracer/testing:cc_library",
        "//src/stirling/source_connectors/socket_tracer/testing/container_images:bssl_container",
        "//src/stirling/source_connectors/socket_tracer/testing/container_images:curl_container",
        "//src/stirling/testing:cc_library",
    ],
)

pl_cc_bpf_test(
    name = "netty_tls_trace_bpf_test",
    timeout = "long",
    srcs = ["netty_tls_trace_bpf_test.cc"],
    flaky = True,
    shard_count = 2,
    tags = [
        "no_asan",
        "requires_bpf",
    ],
    deps = [
        ":cc_library",
        "//src/common/testing/test_utils:cc_library",
        "//src/stirling/source_connectors/socket_tracer/testing:cc_library",
        "//src/stirling/source_connectors/socket_tracer/testing/container_images:thrift_mux_server_container",
        "//src/stirling/testing:cc_library",
    ],
)

pl_cc_bpf_test(
    name = "tls_trace_bpf_test",
    timeout = "long",
    srcs = ["tls_trace_bpf_test.cc"],
    flaky = True,
    shard_count = 2,
    tags = [
        "cpu:16",
        "no_asan",
        "requires_bpf",
    ],
    deps = [
        ":cc_library",
        "//src/common/testing/test_utils:cc_library",
        "//src/stirling/source_connectors/socket_tracer/testing:cc_library",
        "//src/stirling/source_connectors/socket_tracer/testing/container_images:curl_container",
        "//src/stirling/source_connectors/socket_tracer/testing/container_images:nginx_openssl_3_0_8_container",
        "//src/stirling/testing:cc_library",
    ],
)

pl_cc_bpf_test(
    name = "dyn_lib_trace_bpf_test",
    timeout = "moderate",
    srcs = ["dyn_lib_trace_bpf_test.cc"],
    flaky = True,
    tags = [
        "cpu:16",
        "no_asan",
        "requires_bpf",
    ],
    deps = [
        ":cc_library",
        "//src/common/testing/test_utils:cc_library",
        "//src/stirling/source_connectors/socket_tracer/testing:cc_library",
        "//src/stirling/source_connectors/socket_tracer/testing/container_images:nginx_openssl_1_1_0_container",
        "//src/stirling/source_connectors/socket_tracer/testing/container_images:ruby_container",
        "//src/stirling/testing:cc_library",
    ],
)

pl_cc_bpf_test(
    name = "go_tls_trace_bpf_test",
    timeout = "long",
    srcs = ["go_tls_trace_bpf_test.cc"],
    flaky = True,
    tags = [
        "cpu:16",
        "no_asan",
        "requires_bpf",
    ],
    deps = [
        ":cc_library",
        "//src/common/exec:cc_library",
        "//src/common/testing/test_utils:cc_library",
        "//src/stirling/source_connectors/socket_tracer/testing:cc_library",
        "//src/stirling/source_connectors/socket_tracer/testing/container_images:go_1_18_tls_server_container",
        "//src/stirling/source_connectors/socket_tracer/testing/container_images:go_1_19_tls_server_container",
        "//src/stirling/source_connectors/socket_tracer/testing/container_images:go_1_20_tls_server_container",
        "//src/stirling/source_connectors/socket_tracer/testing/container_images:go_1_21_tls_server_container",
        "//src/stirling/source_connectors/socket_tracer/testing/container_images:go_1_22_tls_server_container",
        "//src/stirling/source_connectors/socket_tracer/testing/container_images:go_1_23_tls_client_container",
        "//src/stirling/source_connectors/socket_tracer/testing/container_images:go_1_23_tls_server_container",
        "//src/stirling/source_connectors/socket_tracer/testing/container_images:go_1_24_tls_client_container",
        "//src/stirling/source_connectors/socket_tracer/testing/container_images:go_1_24_tls_server_container",
        "//src/stirling/source_connectors/socket_tracer/testing/container_images:go_boringcrypto_tls_client_container",
        "//src/stirling/source_connectors/socket_tracer/testing/container_images:go_boringcrypto_tls_server_container",
        "//src/stirling/testing:cc_library",
    ],
)

pl_cc_bpf_test(
    name = "amqp_trace_bpf_test",
    timeout = "moderate",
    srcs = ["amqp_trace_bpf_test.cc"],
    tags = ["requires_bpf"],
    deps = [
        ":cc_library",
        "//src/common/testing/test_utils:cc_library",
        "//src/stirling/source_connectors/socket_tracer/testing:cc_library",
        "//src/stirling/source_connectors/socket_tracer/testing/container_images:rabbitmq_consumer_container",
        "//src/stirling/source_connectors/socket_tracer/testing/container_images:rabbitmq_container",
        "//src/stirling/source_connectors/socket_tracer/testing/container_images:rabbitmq_producer_container",
    ],
)

pl_cc_bpf_test(
    name = "mongodb_trace_bpf_test",
    timeout = "moderate",
    srcs = ["mongodb_trace_bpf_test.cc"],
    tags = ["requires_bpf"],
    deps = [
        ":cc_library",
        "//src/common/testing/test_utils:cc_library",
        "//src/stirling/source_connectors/socket_tracer/testing:cc_library",
        "//src/stirling/source_connectors/socket_tracer/testing/container_images:mongodb_client_container",
        "//src/stirling/source_connectors/socket_tracer/testing/container_images:mongodb_container",
        "//src/stirling/testing:cc_library",
    ],
)
